import json
import math


start_point = (0,0,1)  # 起点
drop_point = (10.97,23.77,0)  # 落地点
t = 2  # 飞行时间，单位秒，重起点到落点
t_after_bounce = 0.25 # 反弹后飞行时间，单位秒
g = 9.8 # 重力加速度
bounce_multiplier = 0.8 # 反弹系数
fps = 60 # 帧率
radius = 0.03 # 落地点的半径,单位米
replay_speed = 0.2 # 回放速度



def get_circle_outline(center, radius):
    data = []
    for i in range(30):
        x = center[0] + radius * math.cos(i * 2 * math.pi / 30)
        y = center[1] + radius * math.sin(i * 2 * math.pi / 30)
        z = center[2]
        data.append((x, y, z))
    return data

outline = get_circle_outline(drop_point, radius = radius)  # 落地点的轮廓



DUAL_WIDTH = 10.97
SINGLE_WIDTH = 8.23
HEIGHT = 23.77
SINGLE_DUAL_GAP = (DUAL_WIDTH - SINGLE_WIDTH) / 2
SERVICE_LINE_TO_BASE_LINE = 5.84

vz = t*g/2 # 初速度
vx = 10.97/t # 水平速度
vy = 23.77/t # 垂直速度
speed = math.sqrt(vx**2 + vy**2 + vz**2) * 3.6  # Convert from m/s to km/h
print("飞行速度, vx %s km/h, vy %s km/h, vz %s km/h, 合计 %s km/h" % (vx * 3.6, vy * 3.6, vz * 3.6, speed))

data = {
    "track_name":"Track 1",
    "track_type":"Track Type 1",
    "track_list":[],
    "dpi":"2k",
    "speed": str(replay_speed) + "x"
}
print("轨迹：")
for i in range(int(t*fps)):
    t0 = i * 1.0/fps
    x = vx * t0
    y = vy * t0
    z = vz * t0 - g * t0 * t0 / 2
    data["track_list"].append({
        "coord":[x, y, z] })
    print(x, y, z)

vz = vz - g * t0

data["track_list"][-1]["is_drop"] = True
data["track_list"][-1]["outline"] =  get_circle_outline(drop_point, radius = 0.03)

vx = bounce_multiplier * vx
vy = bounce_multiplier * vy
vz = bounce_multiplier * -vz

x0,y0,z0 = drop_point
for i in range(int(fps*t_after_bounce)):
    t0 = i * 1.0/fps
    x = x0 + vx * t0
    y = y0 + vy * t0
    z = z0 + vz * t0 - g * t0 * t0 / 2
    data["track_list"].append({
        "coord":[x, y, z] })
    print(x, y, z)

with open("track.json", "w") as f:
    string = json.dumps(data)
    f.write(string)





